package com.cyou.nad.tag.table.pagebar;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import com.cyou.nad.dao.Pagination;





/**
 * 分页标签
 * @author lzm 
 */
public class PageBarTag extends TagSupport {
	
	private static final long serialVersionUID = 7552299642713175771L;
	
	public static PageBar defaultBar = new JdbcPageBar();

	/**
	 * 生成标签的方式
	 */
	private PageBar bar;
	/**
	 * 生成标签的方式 默认
	 */
	

	/**
	 * 数据源
	 */
	
	private Pagination page;

	/**
	 * 翻页对应的url
	 */
	private String formAction;

	@Override
	public int doEndTag() throws JspException {
		if (bar == null)
			bar = defaultBar;
		if (page == null)
			throw new JspException("PageBarTag 不能没有设置page对象");
		if (StringUtils.isBlank(formAction)) {
			formAction = ((HttpServletRequest) (pageContext.getRequest())).getRequestURI();
		}
		String html = bar.getHTML(((HttpServletRequest) (pageContext.getRequest())), page, formAction);
		try {
			pageContext.getOut().print(html);
		} catch (IOException e) {
			throw new JspException("PageBarTag"+ e.getMessage(),e);
		}
		return EVAL_PAGE;
	}

	public void setBar(PageBar bar) {
		this.bar = bar;
	}

	public void setPage(Pagination page) {
		this.page = page;
	}

	public void setAction(String formAction) {
		this.formAction = formAction;
	}

}
